﻿using System;
using System.Collections.Generic;
using KPIS.DBM;
using System.Configuration;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.DAL
{
    public class D_Contact
    {
        DBManager dbManager = new DBManager(DataProvider.MySQL, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        public IList<M_Contact> GetList()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_Contact> infoList = new List<M_Contact>();
            try
            {
                dbManager.Open();
                strSQL =  " SELECT icm_con_seq,icm_con_name_th,icm_con_name_eng "
                         +" FROM icm_mas_contact "
                         +" WHERE record_status<>'D' ";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_Contact info = new M_Contact();
                        info.icm_con_seq = Convert.ToInt32(drc[i]["icm_con_seq"]);
                        info.icm_con_name_th = drc[i]["icm_con_name_th"].ToString();
                        info.icm_con_name_eng = drc[i]["icm_con_name_eng"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_Contact> GetListFilterByName(string icm_con_name_th)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_Contact> infoList = new List<M_Contact>();
            try
            {
                dbManager.Open();
                strSQL = " SELECT icm_con_seq,icm_con_name_th,icm_con_name_eng "
                         + " FROM icm_mas_contact "
                         + " WHERE record_status<>'D' and icm_con_name_th LIKE '%' '" + icm_con_name_th + "'  '%'";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_Contact info = new M_Contact();
                        info.icm_con_seq = Convert.ToInt32(drc[i]["icm_con_seq"]);
                        info.icm_con_name_th = drc[i]["icm_con_name_th"].ToString();
                        info.icm_con_name_eng = drc[i]["icm_con_name_eng"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

    }
}
